/********************************************************************************
Copyright (C), Sinowealth Electronic. Ltd.
Author: 	Sino
Version: 	V0.0
Date: 		2014/05/30
History:
	V0.0		2014/05/30		 Preliminary
********************************************************************************/
#include "system.h"

/*******************************************************************************
Function: 
Description:  INT45
Input:	 	
Output: 
Others:
*******************************************************************************/
void InterruptINT4(void) interrupt 10
{
	_push_(INSCON);
    BANK0_SEL;

	if(IF45)		                   //AFE ALARM
	{
		IF45 = 0;
		if(bPDFlg||bIdleFlg)
		{
			InitClk();
			bWakeupFlg = 1;
		}
		bAlarmFlg = 1;
	}
	if(IF46)
	{
		IF46 = 0;
		if(bPDFlg || bIdleFlg)			   //WakeUp
		{
			InitClk();	 	
			bWakeupFlg = 1;	
		}
	}

	_pop_(INSCON);
}



/*******************************************************************************
Function: InterruptTimer3()
Description:  20ms
Input:	 	
Output: 
Others:
*******************************************************************************/
void InterruptTimer3(void)	interrupt	5
{
	static U8 xdata OnTime = 0;
	static U8 xdata OffTime = 0;
	
	_push_(INSCON);

	BANK1_SEL;
	TL3 = 0xCD;		 
	TH3 = 0xFC;			//25mS
 	TF3 = 0;
	BANK0_SEL;

    bTimer25msFlg = 1;

#if 0
    if(bLEDChgFlg)						//charging flicker led 25*20=500mS
    {
        if(++ucLedFlickCnt >= 20)
        {
            ucLedFlickCnt = 0;
			LED_CHARGE ^= 1;
        }
    }
	else
	{
		LED_CHARGE = 0;
	}

    if(bBLEOPEN)						// BLE flicker 25*10=250mS
    {
        if(++ucBLEFlickCnt >= 10)
        {
            ucBLEFlickCnt = 0;
			LED1 ^= 1;
			LED2 ^= 1;
        }
    }
	else
	{
		LED1 = 0;
		LED2 = 0;
	}	
#else
	if(OnTime != 0)					//LED Flick timer
	{
		OnTime--;
		LED_RUN = 1;
	}else if(OffTime != 0)
	{
		OffTime--;
		LED_RUN = 0;
	}else
	{
		OnTime = ucLedOnTime;
		OffTime = ucLedOffTime;
	}
#endif
	
	if(++ucTimer3Cnt >= 40)					  //1S
	{
		ucTimer3Cnt = 0;
		bTimer1sFlg = 1;
	}

	if(++ucUartTimeoutCnt >= 8)			//If not Uart communication within 200ms, then clear 
	{
		REN = 1;	
		ucUartBufPT = 0;
	}
	if(++ucUart1TimeoutCnt >= 8)			//If not Uart1 communication within 200ms, then clear 
	{
		BANK1_SEL;REN1=1;BANK0_SEL;
		ucUart1BufPT = 0;
	}
	if(++ucUart2TimeoutCnt >= 8)			//If not Uart2 communication within 200ms, then clear 
	{
		BANK1_SEL;REN2=1;BANK0_SEL;
		UART485_DE = 0;
		ucUart2BufPT = 0;
	}
	ScanKey();						  //Scankey not keyprocess ,in scankey will judge if have press key and set keyflag
	
	_pop_(INSCON);
}


//void InterruptINT3(void) interrupt 11	 
//{
//	IE3 = 0;			   
//}